package br.ifrn.diacin.ctrlAula.integracao.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import br.ifrn.diacin.ctrlAula.integracao.model.Ambiente;
import br.ifrn.diacin.ctrlAula.integracao.model.Reserva;

@Stateless
public class ReservaDAO extends GenericDAO {

	public ReservaDAO() {
		super();
	}

	public List<Reserva> listarReservas(Ambiente ambiente, Date date) {
		java.sql.Date dataSQL = new java.sql.Date(date.getTime());
		String data = "'" + dataSQL.toString() + "'";
		Query query = getEm().createNativeQuery(
				"SELECT * FROM tb_reserva r WHERE r.fk_ambiente = "
						+ ambiente.getId() + " AND r.dz_reserva = " + data,
				Reserva.class);
		@SuppressWarnings("unchecked")
		List<Reserva> retorno = query.getResultList();
		return retorno;
	}

	public List<Reserva> listarReservasNaoConfirmadas() {
		Calendar c = new GregorianCalendar();
		java.sql.Date dataSQLInicio = new java.sql.Date(c.getTime().getTime());
		c.add(Calendar.DAY_OF_MONTH, 8);
		java.sql.Date dataSQLFim = new java.sql.Date(c.getTime().getTime());
		String dataInicio = "'" + dataSQLInicio.toString() + "'";
		String dataFim = "'" + dataSQLFim.toString() + "'";
		Query query = getEm().createNativeQuery(
				"SELECT * FROM tb_reserva AS r WHERE r.st_confirmado = false AND r.dz_reserva > "
						+ dataInicio + " AND r.dz_reserva < " + dataFim + " ORDER BY r.dz_reserva",
				Reserva.class);
		@SuppressWarnings("unchecked")
		List<Reserva> retorno = query.getResultList();
		return retorno;
	}
}
